<?php
/**
 * Created by PhpStorm.
 * User: alei
 * Date: 11/4/2017 AD
 * Time: 16:17
 */
namespace App\Http\Controllers\MobileApi;

use Storage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Mysql\pre_ct_task;
use App\Models\Mysql\ctranklist;

class RanklistController extends Controller
{
	use GlobalData;
	const ROUTE_PREFIX = '/api/*';

	protected $request;

	protected static $forumNameMapFID;

	protected $curTime;



	/**
	 * 创建新的Ranklist控制器实例
	 * @param \Illuminate\Http\Request $request
	 */
	public function __construct(Request $request)
	{
		$this->request = $request;
		$this->error = [];
		$this->curTime = time();

		self::$forumNameMapFID = $this->getForumNameMapFID();

		if(config('app.debug')) {
			error_reporting(E_ALL | E_STRICT);
		}
	}

	public function get()
    {
        $p = $_REQUEST;
        $rankListType =isset($p['rankListType']) ?  addslashes(trim($p['rankListType'])) : '';
		//test case:api/ranklist/get?type=abc
		if (empty($rankListType)){
            $rankListData = array(
                'code' => 5,
                'msg' => '参数不全',
            );
            exit(json_encode($rankListData));			
		}
		
		// translateTotal:翻译总榜，translateLatest:最近翻译榜，voteTotal:投票总榜，voteLatest：最近投票榜
		//test case: api/ranklist/get?rankListType=abc
		if (!in_array($rankListType,array('translateTotal','translateLatest','voteTotal','voteLatest'))){
            $rankListData = array(
                'code' => 5,
                'msg' => '参数错误，非法的排行榜类型',
            );
            exit(json_encode($rankListData));			
		}

		if (in_array($rankListType,array('translateLatest','voteLatest'))){
			$ct_taskinfo = pre_ct_task::getMostRecentCompletedTask();
			if (empty($ct_taskinfo)){
				$rankListData = array(
					'code' => 200,
					'msg' => '亲，暂无数据',
					'rankListDataEmptyUrl' => config('applqyw.discuz_server_url').'template/comiis_nby/img/lqyw_jing_qing_qi_dai.png',
				);
            exit(json_encode($rankListData));
			}
			//echo print_r($ct_taskinfo,true);
		}
		
		$start=0;
		$countPerPage=20;
		
		switch($rankListType){
			case 'translateTotal':
				$rankListData=ctranklist::getTranslateTotalRankListData($start,$countPerPage);
				break;
				
			case 'translateLatest':
				$rankListData=ctranklist::getTranslateLatestRankListData($ct_taskinfo[0]->{'taskid'},$start,$countPerPage);
				break;
				
			case 'voteTotal':
				$rankListData=ctranklist::getVoteTotalRankListData($start,$countPerPage);
				break;
				
			case 'voteLatest':
				$rankListData=ctranklist::getVoteLatestRankListData($ct_taskinfo[0]->{'polltid'},$start,$countPerPage);
				break;
		}
		
		if (empty($rankListData)){
			$rankListData = array(
				'code' => 200,
				'msg' => '亲，暂无数据',
				'rankListDataEmptyUrl' => config('applqyw.discuz_server_url').'template/comiis_nby/img/lqyw_jing_qing_qi_dai.png',
			);
			exit(json_encode($rankListData));
		}

		$myRank = -1;
		$myCredits = 0;
		
		$i=1;
		$curUser = getCurUser();
		$curUid = empty($curUser) ? 0 : $curUser['uid'];
		foreach($rankListData as $item){
			$item->{'avatarUrl'} = avatarUrl($item->{'uid'});
			if($curUid) {
				if ($curUid==$item->{'uid'}){
					$myCredits = $item->{'credits'};
					$myRank = $i;
				} 
			}
			$i=$i+1;
		}
		
        $rankListData = array(
            'code' => 200,
            'msg' => '排行榜数据返回成功',
			'myRank' => $myRank,
			'myCredits' => $myCredits,
            'rankListData' => $rankListData,
        );
        exit(json_encode($rankListData));			

    }

}


